package alp.starcode.nudtportal.framework.util;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimeComparisonUtil {

    /**
     * 比较当前时间是否再指定时间区间内
     *
     * @param startTime 开始时间-时分秒
     * @param endTime   结束时间-时分秒
     * @return
     */
    public static boolean compNowTime(String startTime, String endTime) {
        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        String hour_str = String.valueOf(hour);
        String minute_str = String.valueOf(minute);
        String second_str = String.valueOf(second);

        String nowTime = hour_str + ":" + minute_str + ":" + second_str;

        boolean startT = compTime(nowTime, startTime);
        boolean endT = compTime(nowTime, endTime);
        if (startT && !endT) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 比较两个时间 时分秒 大小
     *
     * @param s1
     * @param s2
     * @return
     */
    public static boolean compTime(String s1, String s2) {
        try {
            if (s1.indexOf(":") < 0 || s1.indexOf(":") < 0) {
                System.out.println("格式不正确");
            } else {
                String[] array1 = s1.split(":");
                int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60 + Integer.valueOf(array1[2]);
                String[] array2 = s2.split(":");
                int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60 + Integer.valueOf(array2[2]);
                return total1 - total2 > 0 ? true : false;
            }
        } catch (NumberFormatException e) {
            return true;
        }
        return false;

    }
}
